﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class EditDepositHeaderModel
    {
        public DepositHeader Header { get; set; }
        public DepositHeaderDTO HeaderDTO { get; set; }
        public DepositHeaderScreenData ScreenData { get; set; }

        public RatingHistory RatingHistory { get; set; }

        public IList<DepositTransaction> Transactions
        {
            set
            {
                IList<DepositTransaction> data = value;
                if (data != null && data.Count > 0)
                {
                    TransactionAdaters.Clear();
                    foreach (var item in data)
                    {
                        TransactionAdaters.Add(new DepositTransactionAdapter(item, HeaderDTO));
                    }
                }
            }
        }


        public IList<DepositTransactionAdapter> TransactionAdaters { get; set; }
        public IList<Bank> BankBranchs { get; set; }

        public EditDepositHeaderModel()
        {
            Init();
        }
        void Init()
        {
            if (null == Header)
                Header = new DepositHeader();
            if (TransactionAdaters == null)
                TransactionAdaters = new List<DepositTransactionAdapter>();
            if (RatingHistory == null)
                RatingHistory = new RatingHistory();
            if (HeaderDTO == null)
                HeaderDTO = new DepositHeaderDTO();
            if (BankBranchs == null)
                BankBranchs = new List<Bank>();

            HeaderDTO.Header = Header;
            HeaderDTO.Rating = RatingHistory;

            var defaultItem = WebUtil.SelectListSelectDefault();
            CompanyList = new BindingList<SelectListItem>() { defaultItem };
            DepositTypeList = new BindingList<SelectListItem>() { defaultItem };
            FinanceInstrumentTypeList = new BindingList<SelectListItem>() { defaultItem };
            BankList = new BindingList<SelectListItem>() { defaultItem };
            BankBranchList = new BindingList<SelectListItem>() { defaultItem };
            IssueRatingList = new BindingList<SelectListItem>() { defaultItem };
            IssuerRatingList = new BindingList<SelectListItem>() { defaultItem };
            GuarantorRatingList = new BindingList<SelectListItem>() { defaultItem };
            CreditAgencyIssueList = new BindingList<SelectListItem>() { defaultItem };
            CreditAgencyIssuerList = new BindingList<SelectListItem>() { defaultItem };
            CreditAgencyIssueGuarantorList = new BindingList<SelectListItem>() { defaultItem };
            InvolvedPartyTypeNameList = new BindingList<SelectListItem>() { defaultItem };
        }
        public static EditDepositHeaderModel BuildModel(DepositHeaderScreenData data)
        {

            var model = new EditDepositHeaderModel();
            model.ScreenData = data;

            if (data.HeaderDTO != null)
            {
                model.HeaderDTO = data.HeaderDTO;

                if (data.HeaderDTO.Header != null)
                    model.Header = data.HeaderDTO.Header;
                if (data.HeaderDTO.Transactions != null)
                    model.Transactions = data.HeaderDTO.Transactions;
                if (data.HeaderDTO.Rating != null)
                    model.RatingHistory = data.HeaderDTO.Rating;

                var involved = data.HeaderDTO.InvolvedPartyType;
                model.SelectedInvolvedPartyType = involved.InvolvedPartyTypeId;
                model.InvolvedPartyTypeNO = involved.InvolvedPartyTypeCode;
                model.InvolvedPartyTypeReferance = involved.InvolvedPartyTypeReference;
            }


            if (data.Companies != null || data.Companies.Any())
            {
                foreach (var selectListItem in data.Companies)
                {
                    model.CompanyList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.CompanyNameTh, Value = selectListItem.CompanyId.ToString() });
                }
            }
            if (data.DepositTypes != null || data.DepositTypes.Any())
            {
                foreach (var selectListItem in data.DepositTypes)
                {
                    model.DepositTypeList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.AssetClassEN, Value = selectListItem.AssetClassID.ToString() });
                }
            }
            if (data.FinanceTypes != null || data.FinanceTypes.Any())
            {
                foreach (var selectListItem in data.FinanceTypes)
                {
                    model.FinanceInstrumentTypeList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.ValueEN, Value = selectListItem.ID.ToString() });
                }
            }

            if (data.Banks != null || data.Banks.Any())
            {
                foreach (var selectListItem in data.Banks)
                {
                    model.BankList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.BankNameTh, Value = selectListItem.BankCode });
                }
            }

            if (data.Branchs != null && data.Branchs.Any())
            {
                foreach (var selectListItem in data.Branchs)
                {
                    model.BankBranchList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.BankNameTh, Value = selectListItem.BankBranchCode });
                }
            }

            if (data.InvolvedPartyTypes != null || data.InvolvedPartyTypes.Any())
            {
                foreach (var selectListItem in data.InvolvedPartyTypes)
                {
                    model.InvolvedPartyTypeNameList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.InvolvedPartyTypeName, Value = selectListItem.InvolvedPartyTypeId.ToString() });
                }
            }
            if (data.IssueRatings != null || data.IssueRatings.Any())
            {
                foreach (var selectListItem in data.IssueRatings)
                {
                    model.IssueRatingList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.CreditAgencyIssueRating, Value = selectListItem.CreditAgencyIssueRating });
                }
            }
            if (data.IssueRatings != null || data.IssueRatings.Any())
            {
                foreach (var selectListItem in data.IssueRatings)
                {
                    model.CreditAgencyIssueList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.CreditAgencyIssue, Value = selectListItem.CreditAgencyIssue });
                }
            }
            if (data.IssueRatings != null || data.IssueRatings.Any())
            {
                foreach (var selectListItem in data.IssueRatings)
                {
                    model.IssuerRatingList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.CreditAgencyIssueRating, Value = selectListItem.CreditAgencyIssueRating });
                }
            }
            if (data.IssueRatings != null || data.IssueRatings.Any())
            {
                foreach (var selectListItem in data.IssueRatings)
                {
                    model.CreditAgencyIssuerList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.CreditAgencyIssue, Value = selectListItem.CreditAgencyIssue });
                }
            }
            if (data.IssueRatings != null || data.IssueRatings.Any())
            {
                foreach (var selectListItem in data.IssueRatings)
                {
                    model.GuarantorRatingList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.CreditAgencyIssueRating, Value = selectListItem.CreditAgencyIssueRating });
                }
            }
            if (data.IssueRatings != null || data.IssueRatings.Any())
            {
                foreach (var selectListItem in data.IssueRatings)
                {
                    model.CreditAgencyIssueGuarantorList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.CreditAgencyIssue, Value = selectListItem.CreditAgencyIssue });
                }
            }

            return model;
        }

        #region dropdown list

        [Required(ErrorMessage = "โปรดระบุ")]
        public int? SelectedCompany
        {
            get { return Header.DepositCompanyID; }
            set { Header.DepositCompanyID = value; }
        }
        public IList<SelectListItem> CompanyList { get; set; }

        public int? SelectedDepositType
        {
            get { return Header.AssetClassID; }
            set { Header.AssetClassID = value; }
        }
        public IList<SelectListItem> DepositTypeList { get; set; }

        public int? SelectedFinanceInstrumentType
        {
            get { return Header.FinancialInstrumentTypeID; }
            set { Header.FinancialInstrumentTypeID = value; }
        }
        public IList<SelectListItem> FinanceInstrumentTypeList { get; set; }

        public string SelectedBank
        {
            get { return Header.BankCode; }
            set { Header.BankCode = value; }
        }
        public IList<SelectListItem> BankList { get; set; }

        public string SelectedBankBranch
        {
            get { return Header.BranchCode; }
            set { Header.BranchCode = value; }
        }
        public IList<SelectListItem> BankBranchList { get; set; }

        public string IssueRating
        {
            get { return RatingHistory.IssueRating; }
            set { RatingHistory.IssueRating = value; }
        }
        public IList<SelectListItem> IssueRatingList { get; set; }

        public string IssuerRating
        {
            get { return RatingHistory.IssuerRating; }
            set { RatingHistory.IssuerRating = value; }
        }
        public IList<SelectListItem> IssuerRatingList { get; set; }

        public string GuarantorRating
        {
            get { return RatingHistory.GuarantorRating; }
            set { RatingHistory.GuarantorRating = value; }
        }
        public IList<SelectListItem> GuarantorRatingList { get; set; }

        public string CreditAgencyIssue
        {
            get { return RatingHistory.IssueCreditAgency; }
            set { RatingHistory.IssueCreditAgency = value; }
        }
        public IList<SelectListItem> CreditAgencyIssueList { get; set; }

        public string CreditAgencyIssuer
        {
            get { return RatingHistory.IssuerCreditAgency; }
            set { RatingHistory.IssuerCreditAgency = value; }
        }
        public IList<SelectListItem> CreditAgencyIssuerList { get; set; }

        public string CreditAgencyIssueGuarantor
        {
            get { return RatingHistory.GuarantorCreditAgency; }
            set { RatingHistory.GuarantorCreditAgency = value; }
        }
        public IList<SelectListItem> CreditAgencyIssueGuarantorList { get; set; }


        private int _involvedPartyTypeID;
        public int SelectedInvolvedPartyType
        {
            get { return _involvedPartyTypeID; }
            set
            {
                _involvedPartyTypeID = value;
            }
        }

        public IList<SelectListItem> InvolvedPartyTypeNameList { get; set; }



        #endregion

        //#region adapter
        public int HeaderID
        {
            get
            {
                return Header.DepositHeaderID;
            }

            set
            {
                Header.DepositHeaderID = value;
            }
        }

        public string InvolvedPartyTypeNO { get; set; }
        public string InvolvedPartyTypeReferance { get; set; }

        private bool _isTierISelected;
        public bool IsTierISelected
        {
            get { return _isTierISelected; }
            set
            {
                _isTierISelected = value;
            }
        }

        private bool _isTierIiSelected;
        public bool IsTierIISelected
        {
            get { return _isTierIiSelected; }
            set
            {
                _isTierIiSelected = value;
            }
        }

        private bool _isLowLiquiditySelected;
        public bool IsLowLiquiditySelected
        {
            get { return _isLowLiquiditySelected; }
            set
            {
                _isLowLiquiditySelected = value;
            }
        }


        //#endregion

        public String SaveBtn { get; set; }
        //public String CancelBtn { get; set; }
        //public String EditBtn { get; set; }
        public bool UpdateMode { get; set; }

    }


}